import { _decorator, UITransform } from 'cc';
import { DIRECTION_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_ENUM, PLAYER_DEAD_TYPE } from '../../Enum';
import { TILE_WEIGHT, TILE_HEIGHT } from '../Tile/TileItem';
import { EntiyManager } from '../../common/EnityManager';
import {BurstStateMachine } from './BurstStateMachine';
import { EventManager } from '../../RunTime/EventManager';
import { DataManager } from '../../RunTime/DataManager';
import { IEntity } from '../../Levels';
const { ccclass, property } = _decorator;


@ccclass('BurstManager')
export class BurstManager extends EntiyManager {

  async init(param: IEntity){
    this.fsm = this.addComponent(BurstStateMachine)
    await this.fsm.init()
    super.init(param)
    this.getComponent(UITransform).setContentSize(TILE_WEIGHT, TILE_HEIGHT)
    this.node.setPosition(this.x * TILE_WEIGHT, -this.y * TILE_HEIGHT)

    EventManager.getInstance().on(EVENT_ENUM.PLAYER_MOVE_END, this.OnBurst, this)
    EventManager.getInstance().on(EVENT_ENUM.PLAYER_TURN, this.OnBurst, this)
  }

  OnBurst(){

    if (this.state == ENTITY_STATE_ENUM.DEAD) {
      return
    }
    const {x, y} = DataManager.getInstance().player
    if (this.x == x && this.y == y) {
      if (this.state == ENTITY_STATE_ENUM.IDLE){
        this.state = ENTITY_STATE_ENUM.ATTACK
      } else {
        this.state = ENTITY_STATE_ENUM.DEAD
        EventManager.getInstance().emit(EVENT_ENUM.PLAYER_DIE, PLAYER_DEAD_TYPE.BURST_ATTACK)
      }
    }

  }






}
